<template>
  <!-- 能效分析树形控件 -->
  <Row>
    <Row style="margin:10px 0;">
      <i-input
        icon="ios-search"
        v-model="title"
        :placeholder="placeholder"
        :disabled="token.groupCode=='0202'"
      ></i-input>
    </Row>

    <Tree
      v-if="baseDataList.length>0"
      :data="baseDataList"
      :show-checkbox="showCheckbox"
      :check-strictly="associated"
      @on-select-change="selectChange"
      @on-check-change="checkChange"
    ></Tree>
    <img
      v-if="baseDataList.length==0"
      src="~@/assets/img/efficiencyAnalysis/zwsj.png"
      width="80%"
      style="position: relative;left: 10%;"
    />
  </Row>
</template>

<script>
import { groupAreaCust } from "@/api/efficiency.js";
import util from "@/libs/util.js";
export default {
  props: {
    showCheckbox: {
      type: Boolean,
      default: false
    },
    checkNode: {
      type: Function,
      default: null
    },
    checkNodes: {
      type: Function,
      default: null
    },
    checkType: {
      type: Number,
      default: 1
    },
    placeholder: {
      type: String,
      default: ""
    },
    sizeLimit: {
      type: Boolean,
      default: true
    },
    associated: {
      type: Boolean,
      default: true
    },
    selectAll: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      baseDataList: [],
      title: "",
      checkList: [],
      checkState: false,
      token: null,
      checkDouble: [],
      selectOne: []
    };
  },
  computed: {},
  watch: {
    title(val) {
      groupAreaCust({
        cliqueCustNo: val,
        custId: this.$route.query.custId || util.token.initToken().custId,
        cliqueId: this.$route.query.cliqueId || util.token.initToken().cliqueId,
        type: "1"
      }).then(res => {
        if (res.code == 200) {
          let baseData = this.filterNode([res.result], 0);
          this.baseDataList = baseData;
        } else {
          this.baseDataList = [];
        }
      });
    }
  },
  methods: {
    selectChange(nodes, node) {
      if (this.showCheckbox) {
        if (!node.disableCheckbox) {
          if (node.checked) {
            node.checked = false;
            this.checkList = this.checkList.filter(item => {
              return item.title != node.title;
            });
            this.checkChange(this.checkList, node);
            return;
          }

          if (this.sizeLimit) {
            if (this.checkList.length > 3) {
              this.$Message.info("最多只能选择四个节点");
              return;
            }
          }
          if (nodes.length == 0 && !node.checked) {
            node.checked = true;
            this.checkList.push(node);
            this.checkChange(this.checkList, node);
          } else {
            node.checked = true;
            this.checkList.push(node);
            this.checkChange(this.checkList, node);
          }
        }
      } else {
        this.checkNode(nodes, node);
      }
    },
    checkChange(nodes, node) {
      this.checkState = true;
      if (this.sizeLimit) {
        let disabledList = [];
        nodes.map(item => {
          if (item.disableCheckbox) {
          } else {
            disabledList.push(item);
          }
        });
        if (disabledList.length > 4) {
          this.$Message.info("最多只能选择四个节点");
          nodes = nodes.filter(item => {
            if (item.title == node.title) {
              node.checked = false;
            }
            return item.title != node.title;
          });
        }
      } else {
        nodes.map(item => {
          if (item.disableCheckbox) {
            item.checked = false;
          } else {
            item.checked = true;
          }
        });
      }

      this.checkList = nodes;
      let list = JSON.parse(JSON.stringify(this.baseDataList));
      this.baseDataList = [];
      this.baseDataList = JSON.parse(JSON.stringify(list));
      this.checkNodes(nodes, node);
    },
    filterNode(list, cliqueId) {
      list.map(res => {
        if (this.checkState == true && this.showCheckbox == true) {
          this.checkList.map(item => {
            if (item.dateType == "1") {
              if (item.cliqueId == res.cliqueId) {
                res.checked = true;
              }
            } else if (item.dateType == "2") {
              if (item.orgNo == res.orgNo) {
                res.checked = true;
              }
            } else if (item.dateType == "3") {
              if (item.custId == res.custId) {
                res.checked = true;
              }
            }
          });
        } else {
          if (this.selectAll) {
            res.checked = true;
            this.checkList.push(res);
          } else if (
            this.showCheckbox &&
            this.checkDouble.length < 2 &&
            res.custId != null &&
            res.custId != "null"
          ) {
            res.checked = true;
            this.checkDouble.push(res);
          } else if (
            !this.showCheckbox &&
            this.selectOne.length < 1 &&
            res.custId != null &&
            res.custId != "null"
          ) {
            res.selected = true;
            this.selectOne.push(res);
          }
        }
        if (res.cliqueId == null || res.cliqueId == "null") {
          res.cliqueId = cliqueId;
        }
        if (
          "cliqueName" in res &&
          res.cliqueName != null &&
          res.cliqueName != "null"
        ) {
          res.title = res.cliqueName;
        } else if (
          "orgName" in res &&
          res.orgName != null &&
          res.orgName != "null"
        ) {
          res.title = res.orgName;
        } else if (
          "custName" in res &&
          res.custName != null &&
          res.custName != "null"
        ) {
          res.title = res.custName;
        }
      });
      if (this.checkType == 1) {
        list.map(res => {
          res.expand = true;
          if (res.custId != null && res.custId != "null") {
            res.disableCheckbox = true;
          } else {
            res.disableCheckbox = false;
          }
          if (
            "children" in res &&
            res.children != null &&
            res.children.length > 0
          ) {
            this.filterNode(res.children, res.cliqueId);
          }
        });
      } else if (this.checkType == 2) {
        list.map(res => {
          res.expand = true;
          if (res.custId != null && res.custId != "null") {
            res.disableCheckbox = false;
          } else {
            res.disableCheckbox = true;
          }
          if (
            "children" in res &&
            res.children != null &&
            res.children.length > 0
          ) {
            this.filterNode(res.children, res.cliqueId);
          }
        });
      } else if (this.checkType == 3) {
        list.map(res => {
          res.expand = true;
          if (
            "children" in res &&
            res.children != null &&
            res.children.length > 0
          ) {
            this.filterNode(res.children, res.cliqueId);
          }
        });
      }

      return list;
    }
  },
  created() {
    this.token = util.token.initToken();
    groupAreaCust({
      custId: this.$route.query.custId || util.token.initToken().custId,
      cliqueId: this.$route.query.cliqueId || util.token.initToken().cliqueId,
      type: "1"
    }).then(res => {
      if (res.code == 200) {
        let baseData = this.filterNode([res.result], 0);
        if (this.selectAll) {
          this.checkNodes(this.checkList);
        } else if (this.showCheckbox && this.checkDouble.length > 0) {
          this.checkNodes(this.checkDouble);
        } else if (this.selectOne.length > 0) {
          this.selectChange(this.selectOne, this.selectOne[0]);
        }
        this.baseDataList = baseData;
      } else {
        this.baseDataList = [];
      }
    });
  },
  mounted() {},
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>